package com.dms.modules.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dms.common.api.ApiResponse;
import com.dms.modules.community.entity.CommunityCategory;
import com.dms.modules.community.service.CommunityCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "社区分类管理")
@RestController
@RequestMapping("/community/category")
public class CommunityCategoryController {

    @Autowired
    private CommunityCategoryService categoryService;

    @ApiOperation("创建分类")
    @PostMapping
    public ApiResponse<Long> createCategory(@RequestParam String name,
                                           @RequestParam(required = false) String description,
                                           @RequestParam(required = false) String icon,
                                           @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Long categoryId = categoryService.createCategory(name, description, icon, sort);
        return ApiResponse.success(categoryId);
    }

    @ApiOperation("更新分类")
    @PutMapping("/{id}")
    public ApiResponse<Boolean> updateCategory(@PathVariable Long id,
                                              @RequestParam String name,
                                              @RequestParam(required = false) String description,
                                              @RequestParam(required = false) String icon,
                                              @RequestParam(required = false) Integer sort,
                                              @RequestParam(required = false) Integer status) {
        boolean result = categoryService.updateCategory(id, name, description, icon, sort, status);
        return ApiResponse.success(result);
    }

    @ApiOperation("获取分类列表")
    @GetMapping
    public ApiResponse<IPage<CommunityCategory>> getCategoryList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<CommunityCategory> page = new Page<>(pageNum, pageSize);
        IPage<CommunityCategory> categoryPage = categoryService.getCategoryList(page);
        return ApiResponse.success(categoryPage);
    }
} 